Component({
  /**
   * 组件的属性列表
   */
  properties: {
    currDate: null, //当前选中的日期
  },
  /**
   * 页面的初始数据
   */
  data: {
    // infoData:[],//日历模板显示
    // year: 2018,//当前显示年份
    // month: 1,//当前显示月份
    // day:1,//当前显示天数
    // weekText:"",//当前星期
    // showMonth: '2018-03-01',//需要显示月份的日历
    // hiddenRight:true,//是否隐藏下一月按钮
    startClientX: '', //开始触摸时x坐标
    startClientY: '', //开始触摸时y坐标
  },

  /**
   * 生命周期函数--监听页面加载
   */
  ready: function(options) {
    const that = this;

    that.setData({
      showMonth: that.data.currDate
    })
    that.init();
  },

  methods: {
    init: function() {
      const that = this;

      // 初始赋值
      let showMonth = that.time().typeB.split('-');

      that.setData({
        year: Number(showMonth[0]),
        month: Number(showMonth[1]),
        showMonth: showMonth[0] + '-' + showMonth[1] + '-01',
        hiddenRight: (Number(showMonth[0]) === that.time(0, true).year && Number(showMonth[1]) === Number(that.time(0, true).month)),
      })

      that.calendarModel();

    },
    /**
     * 日历模板
     */
    calendarModel: function() {
      const that = this;
      const currDate = that.data.currDate;
      // 获取当月1号是星期几
      let week = that.time(-(that.time().day - 1)).week;
      let tempArr = [];
      // 1号前空白格
      for (let i = 0; i < week; i++) {
        let tempObj = {};
        tempObj.status = 0;
        // 2018.5.14修改（wx:key值不能为空）
        tempObj.day = i - week;
        tempObj.time = i - week;
        tempArr.push(tempObj);
      }

      // 日历
      for (let i = 0; i < 32; i++) {
        let tempObj = {};
        tempObj.time = that.time(i).typeB;
        tempObj.day = that.time(i).day;
        tempObj.year = that.time(i).year;
        tempObj.month = Number(that.time(i).month);
        tempObj.week = that.time(i).week;
        tempObj.weekText = that.time(i).weekText;
        // 每日24点毫秒数是否大于当前毫秒数
        tempObj.status = (that.time(i).msec + that.time().dayMsec) > that.time(1, true).msec ? 0 : 1;
        if (currDate == tempObj.time) {
          tempObj.flag = 1;
          that.setData({
            selTempYear: that.time(i).year,
            selTempMonth: Number(that.time(i).month),
            selTempDay: that.time(i).day,
            selTempWeekText: that.time(i).weekText,
          })
        }
        tempArr.push(tempObj);
        if (that.time(i).day > that.time(i + 1).day) break;
      }
      // console.log(tempArr)
      that.setData({
        infoData: tempArr,
      })
    },

    /**
     * 日历切换
     */
    toggleCanlendar: function(event) {
      const that = this;
      let year = that.data.year;
      let month = that.data.month;
      // 切换类型（上个月，下个月）
      let num = event.currentTarget.dataset.num;
      let tempMonth = month + num;

      // 如果当前显示的月份是当前月份，不可上滑
      if (year === that.time(0, true).year && month === Number(that.time(0, true).month) && num === 1) return;

      if (tempMonth > 12) {
        tempMonth = 1;
        year += num;
      }
      if (tempMonth < 1) {
        tempMonth = 12;
        year += num;
      }

      that.setData({
        year: year,
        month: tempMonth,
        showMonth: year + '-' + (tempMonth > 9 ? tempMonth : '0' + tempMonth) + '-01',
        // hiddenRight: (year === that.time(0, true).year && tempMonth === Number(that.time(0, true).month)),
      })
      that.init();
    },
    /**
     * 选择日期
     */
    changeDay: function(event) {
      const that = this;
      const index = event.currentTarget.dataset.index;
      const item = event.currentTarget.dataset.item;
      let time = item.time;
      let infoData = that.data.infoData;
      let prevIndex = infoData.findIndex((item) => item.flag);
      if (prevIndex >= 0) infoData[prevIndex].flag = 0;
      infoData[index].flag = 1;

      that.setData({
        infoData: infoData,
        selTempYear: infoData[index].year,
        selTempMonth: Number(infoData[index].month),
        selTempDay: infoData[index].day,
        selTempWeekText: infoData[index].weekText,
        currDate: time,
      })

      // that.triggerEvent('selectDay', item)
    },
    /**
     * 关闭日历
     */
    closeModal: function() {
      var that = this;
      that.triggerEvent('closeModal');
    },
    /**
     * 确定日历
     */
    comfirm: function() {
      var that = this;
      var currDate = that.data.currDate;
      that.triggerEvent('selectDay', {
        time: currDate
      })
    },
    /**
     * 获取年月日
     * params n 与当天时间的差值
     */
    time: function(n = 0, isCurrent) {
      const that = this;
      let showMonth = that.data.showMonth;
      let date;

      if (!showMonth || isCurrent) {
        date = new Date();
      } else {
        date = new Date(showMonth);
      }

      date.setDate(date.getDate() + n);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      let week = date.getDay();
      let msec = date.getTime();
      if (month < 10) {
        month = '0' + month;
      }

      let weekText;
      switch (week) {
        case 1:
          weekText = "一";
          break;
        case 2:
          weekText = "二";
          break;
        case 3:
          weekText = "三";
          break;
        case 4:
          weekText = "四";
          break;
        case 5:
          weekText = "五";
          break;
        case 6:
          weekText = "六";
          break;
        case 0:
          weekText = "日";
          break;
      }

      return {
        typeA: month + '月' + day + '日',
        typeB: year + '-' + month + '-' + (day < 10 ? '0' + day : day),
        year: year,
        month: month,
        day: day,
        week: week,
        weekText: weekText,
        msec: msec, //毫秒
        dayMsec: 1000 * 60 * 60 * 24, //一天的毫秒
      };
    },

    /**
     * 触摸开始【2018.06.11去除】
     */
    touchstart: function(event) {
      // console.log(event)
      return;
      const that = this;
      const clientX = event.changedTouches[0].clientX;
      const clientY = event.changedTouches[0].clientY;
      that.setData({
        startClientX: clientX,
        startClientY: clientY
      })
    },
    /**
     * 触摸结束【2018.06.11去除】
     */
    touchend: function(event) {
      // console.log(event)
      retrun;
      const that = this;
      const endClientX = event.changedTouches[0].clientX;
      const endClientY = event.changedTouches[0].clientY;
      const startClientX = that.data.startClientX;
      const startClientY = that.data.startClientY;
      if (!startClientY || !endClientY) return;
      if (startClientY - endClientY > 50) { //跳转下个月
        // event仿照bindtap点击事件传参
        const event = {
          currentTarget: {
            dataset: {
              num: 1
            }
          }
        }
        return that.toggleCanlendar(event)
      } else if (endClientY - startClientY > 50) { //跳回下个月
        // event仿照bindtap点击事件传参
        const event = {
          currentTarget: {
            dataset: {
              num: -1
            }
          }
        }
        return that.toggleCanlendar(event)
      }
    },
  },
})